package com.hyt.it.ogt.download.controller;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.download.config.MonitorBizRunTimeException;
import com.hyt.it.ogt.download.enums.BizStatusCode;
import com.hyt.it.ogt.download.service.IUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "2.用户登录接口", value = "用户登录接口")
@ApiSort(value = 2)
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {
    @Resource
    private IUserService iUserService;

    @ApiOperation(value = "2.1 登录", notes = "返回CODE码：20000-成功，10020101-账号密码错误，10020102-用户没有下载视频权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @GetMapping("login")
    public ApiResponse login(@RequestParam String username, @RequestParam String password, @RequestParam String examCode) {
        try {
            log.info("视频下载服务login用户登录信息:{}, {}, {}", username, password, examCode);
            String token = iUserService.login(username, password, examCode);
            return ApiResponse.builder().code(BizStatusCode.SUCCESS_CODE.getCode()).data(token).build();
        } catch (MonitorBizRunTimeException e) {
            return ApiResponse.builder().code(e.getCode()).data(e.getMessage()).build();
        } catch (Exception e) {
            log.error("", e);
            return ApiResponse.builder().code(BizStatusCode.LOGIN_WRONG_PASSWORD.getCode())
                    .data(BizStatusCode.LOGIN_WRONG_PASSWORD.getMsg()).build();
        }
    }
}
